package nancal.mp.service.jenkins

import nancal.mp.db.mongo.entity.dev.AppDeploySetting
import nancal.mp.db.mongo.entity.dev.JenkinsLog
import nancal.mp.db.mongo.extend.getAppDeploySetting
import nancal.mp.db.mongo.mor
import nbcp.base.extend.AsString
import nbcp.base.extend.HasValue
import nbcp.base.extend.must
import nbcp.base.utils.ResourceUtil
import nbcp.base.utils.UrlUtil

class JenkinsPipeLineStyleScript(val task: JenkinsLog = JenkinsLog()) : JenkinsScript {
    val appDeploySetting: AppDeploySetting
        get() {
            return mor.dev.appDeploySetting.getAppDeploySetting(task.buildInfo.targetEnv)
        }


    val template by lazy {
        return@lazy ResourceUtil.readResourceContentText("devops/jenkins/pipeline-template.xml")
            .must { it.HasValue }
            .elseThrow { "找不到 devops/jenkins/pipeline-template.xml 资源" }
    }

    private fun getStartStage(): String {

        var dockerLoginScript =  ""

        if(appDeploySetting.harborServer.host.HasValue && appDeploySetting.harborServer.userName.HasValue ){
            dockerLoginScript = """
    echo "登录到 harbor: ${appDeploySetting.harborServer.host}"
    echo "${this.appDeploySetting.sudo} docker login -u ${appDeploySetting.harborServer.userName} -p xxx ${appDeploySetting.harborServer.host}"
    ${this.appDeploySetting.sudo} docker login -u ${appDeploySetting.harborServer.userName} -p ${appDeploySetting.harborServer.password} ${appDeploySetting.harborServer.host}
"""
        }


        return """
stage('${this.task.buildInfo.appCnName.AsString(this.task.buildInfo.appName)}') {
            steps {
           
            sh '''#!/usr/bin/env bash
${this.appDeploySetting.sudo} rm -rf * 
${this.appDeploySetting.sudo} chmod a+rw ./ -R
'''

deleteDir()

${
            """
${dockerLoginScript}
echo `date "+%Y-%m-%d %H:%M:%S"`"下载指令..."

function getPwdInDockerPath2(){
    path=${'$'}1
    if [ "${'$'}path" == "" ]; then
        path=`pwd`
    fi
    
    if [ "${appDeploySetting.jenkinsServer.jenkinsHomePathMountToDocker}" == "" ]; then
        echo ${'$'}path
        return ;
    fi
    
    #实际 =  挂载的外部目录 +  当前目录去除 /var/jenkins_home 部分
    echo ${appDeploySetting.jenkinsServer.jenkinsHomePathMountToDocker}${'$'}{path:17}
}

echo "﹍﹎﹍﹎﹍﹎﹎﹎"

${this.appDeploySetting.sudo} docker run --rm --privileged=true ${appDeploySetting.dockerBuildAddHostsParameters} \
   -v `getPwdInDockerPath2`:`pwd`  -w `pwd` \
   ${appDeploySetting.getOpsBaseBashImage()} \
   bash -c "set -x ; wget -q ${appDeploySetting.mpIntegrationApiUrl}/ops/jenkins/cmd/file/${task.id}?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)} -O ops-cmd.zip && unzip -q -o -d ./ ops-cmd.zip && dos2unix *.sh 2>/dev/null && pwd && tree -L 3 ./"
echo "﹊﹉﹊﹉﹊﹉﹊﹊"

""".sudoExecSh()
        }

            }
        }
"""
    }


    override fun genStage(name: String, content: String): String {
        return """
stage('${name}') {
            steps {
          ${
            """
echo "source main.sh " &&  source main.sh

${content}

""".sudoExecSh()
        }
            }
        }  
        """
    }

    /**
     * lib流水线
     */
    override fun getLibJenkinsPipeLine(): String {
        if (appDeploySetting.mpIntegrationApiUrl.isNullOrEmpty()) {
            throw RuntimeException("${appDeploySetting.code} appDeploySetting.mpIntegrationApiHost 不能为空")
        }
        if (appDeploySetting.nexusServer.nexusHttpUrl.isNullOrEmpty()) {
            throw RuntimeException("${appDeploySetting.code} appDeploySetting.nexusServer.hostUrl 不能为空")
        }

        var content = """
pipeline {
    agent any
    environment {
        jobName = "${this.task.buildInfo.serviceName}"
    }
    stages {
        ${getStartStage()}
        ${getCodeStage()}
        ${getCompileStage()}
        ${genStage("完成", """echo 完成 """)}
    }
} 
"""

        return template
            .replace("@pipeline@", content)
            .replace("@description@", this.task.buildInfo.serviceName)
    }

    override fun getNativeJenkinsPipeLine(): String {
        if (appDeploySetting.mpIntegrationApiUrl.isNullOrEmpty()) {
            throw RuntimeException("${appDeploySetting.code} appDeploySetting.mpIntegrationApiHost 不能为空")
        }


        var gitBuildInfo = task.buildInfo;

        var content = """
pipeline {
    agent any
    environment {
        jobName = "${gitBuildInfo.serviceName}"
    }
    stages {
        ${getStartStage()}
        ${getCodeStage()}
        ${getCompileStage()}
        ${getPushState(appDeploySetting.harborServer.host.HasValue)}
        ${unzipDockerTar(appDeploySetting.harborServer.host.HasValue)}
        ${
            genStage(
                "归档", """
${this.appDeploySetting.sudo} bash sys-pre-deploy.sh 
${this.appDeploySetting.sudo} bash sys-post-deploy.sh
"""
            )
        }
    }
} 
"""

        return template
            .replace("@pipeline@", content)
            .replace("@description@", this.task.buildInfo.serviceName)
    }


    override fun getDockerJenkinsPipeLine(): String {
        if (appDeploySetting.mpIntegrationApiUrl.isNullOrEmpty()) {
            throw RuntimeException("${appDeploySetting.code} appDeploySetting.mpIntegrationApiHost 不能为空")
        }

        var content = """
pipeline {
    agent any
    environment {
        jobName = "${this.task.buildInfo.serviceName}"
    }
    stages {
        ${getStartStage()}
        ${getCodeStage()}
        ${getCompileStage()}
        ${getPushState(appDeploySetting.harborServer.host.HasValue)}
        ${unzipDockerTar(appDeploySetting.harborServer.host.HasValue)}
        ${
            genStage(
                "检查启动", """
${this.appDeploySetting.sudo} bash sys-pre-deploy.sh
check-docker-status
${this.appDeploySetting.sudo} bash sys-post-deploy.sh
"""
            )
        }
    }
} 
"""

        return template
            .replace("@pipeline@", content)
            .replace("@description@", this.task.buildInfo.serviceName)
    }


    override fun getK8sJenkinsPipeLine(): String {
        if (appDeploySetting.mpIntegrationApiUrl.isNullOrEmpty()) {
            throw RuntimeException("${appDeploySetting.code} appDeploySetting.mpIntegrationApiHost 不能为空")
        }


        var content = """
pipeline {
    agent any
    environment {
        jobName = "${this.task.buildInfo.serviceName}"
    }
    stages {
        ${getStartStage()}
        ${getCodeStage()}
        ${getCompileStage()}
        ${getPushState(appDeploySetting.harborServer.host.HasValue)}
        ${
            genStage(
                "部署到K8s", """
${this.appDeploySetting.sudo} bash sys-pre-deploy.sh
run-in-docker deploy-k8s
run-in-docker update-nginx-conf
${this.appDeploySetting.sudo} bash sys-post-deploy.sh
"""
            )
        }
        ${
            genStage(
                "检查启动", """
run-in-docker check-k8s-status

echo ""
"""
            )
        }
    }
} 
"""

        return template
            .replace("@pipeline@", content)
            .replace("@description@", this.task.buildInfo.serviceName)

    }
}